/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuyensinh;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author TienNT
 */
public class Users {

    private String username;
    private String password;
    static Connection conn;
    static Statement stm;

    public Users() {
    }

    public Users(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static void opendatabase() throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:tuyensinh");
        stm = conn.createStatement();
    }

    public static void closeDatabase() throws SQLException {
        stm.close();
        conn.close();
    }

    public static Users login(String username, String password) throws SQLException {
        Users user = null;
        opendatabase();
        String query = "select * from Users where username='" + username + "'";
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
            String sPassword = rs.getString("password");
            if (sPassword.equals(password)) {
                user = new Users();
                user.setUsername(username);
                user.setPassword(password);
            }
        }
        return user;
    }
}
